﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoanPrequalificationSystem.Model
{
    public class BankAccount : IBankAccount
    {
        private string _accountNumber;
        private string _bankName;
        private string _name;
        private string _sortCode;


          public BankAccount()
        { }

        public BankAccount(string accountNumber, string bankName, string name, string sortCode)
        {
            _accountNumber = accountNumber;
            _bankName = bankName;
            _name = name;
            _sortCode = sortCode;

            if (GetBrokenRules().Count > 0)
            {
                throw new Exception(String.Format("The bank account you are trying to create contains invalid data. {0}",GetBrokenRulesToString()));
            }
        }

        private string GetBrokenRulesToString() 
        {
            StringBuilder sbBrokenRules = new StringBuilder();
            foreach (BrokenBusinessRule br in GetBrokenRules())
            {
                sbBrokenRules.Append(br.Rule);
            }
            return sbBrokenRules.ToString();
        }
        public string AccountNumber
        {
            get { return _accountNumber; }
        }

        public string BankName
        {
            get { return _bankName; }
        }

        public void Credit(float Amount)
        {
            throw new NotImplementedException();
        }

        public List<BrokenBusinessRule> GetBrokenRules()
        {
            List<BrokenBusinessRule> brokenRules = new List<BrokenBusinessRule>();
            if (String.IsNullOrEmpty(AccountNumber)) { 
            brokenRules.Add(new BrokenBusinessRule("Account Number","An Account Number must be defined for a Bank Account."));
            }
            if (String.IsNullOrEmpty(BankName)) { 
            brokenRules.Add(new BrokenBusinessRule("Bank","Please Specify the name of the bank."));
            }if (String.IsNullOrEmpty(Name)) { 
            brokenRules.Add(new BrokenBusinessRule("Name","Please specify the name of the account holder."));
            }if (String.IsNullOrEmpty(SortCode)) { 
            brokenRules.Add(new BrokenBusinessRule("Sort Code","Sort Code must be defined for a Bank Account."));
            }
            return brokenRules;
        }

        public string Name
        {
            get { return _name; }
        }

        public string SortCode
        {
            get { return _sortCode; }
        }

    }
}
